package com.audaque.springboot.foshanupload.web.springsecuritycas.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 保存请求地址到session,用于认证成功后回调
 */
@Component
@Slf4j
public class RequestUrlFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(
            ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {
        final HttpServletRequest request = (HttpServletRequest) servletRequest;
        final HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        if(session!=null){
            StringBuffer requestURL = request.getRequestURL();
            String requestUrl = requestURL.toString();
            log.debug("请求地址：" + requestUrl);
            session.setAttribute("REQUEST_URL",requestUrl);
        }

        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}

